//
//  WebappBrowserViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-5-18.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "WebappBrowserViewController.h"

@interface WebappBrowserViewController()<UIWebViewDelegate> {
    
}
@end

@implementation WebappBrowserViewController

@synthesize wbWebView,webappLink;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationController.navigationBar setHidden:NO];
    [super customBackButton];
    
    [wbWebView loadHTMLString:Web_Loading_String baseURL:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [wbWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webappLink]]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"start load website");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self refreshBottomView];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//    [wbWebView loadHTMLString:@"暂无内容" baseURL:nil];
    [self refreshBottomView];
}

- (void)refreshBottomView{
    previousButton.enabled = YES;
    nextButton.enabled = YES;
    [refreshButton setHidden:NO];
    if (![wbWebView canGoBack]) {
        previousButton.enabled = NO;
    }
    if (![wbWebView canGoForward]) {
        nextButton.enabled = NO;
    }
}

- (void)goBack {
    if ([wbWebView canGoBack]) {
        [wbWebView goBack];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - Public
- (IBAction)btnAction:(id)sender {
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 101) {
        //后退
        [wbWebView goBack];
    }
    else if (btn.tag == 102) {
        //刷新
        [wbWebView reload];
    }
    else if (btn.tag == 103) {
        //前进
        [wbWebView goForward];
    }
}

@end
